package com.yc.web.controller.system;

import com.yc.common.annotation.Log;
import com.yc.common.constant.HttpStatus;
import com.yc.common.core.domain.entity.SysUser;
import com.yc.common.enums.OperationType;
import com.yc.common.core.domain.AjaxResult;
import com.yc.common.core.domain.model.LoginUser;
import com.yc.framework.web.service.TokenService;
import com.yc.system.domain.vo.RouterVo;
import com.yc.system.service.ISysMenuService;
import com.yc.system.service.ISysPermissionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Api(tags = "菜单管理")
@RestController
@RequestMapping("/menu")
public class SysMenuController {

    @Autowired
    private ISysMenuService menuService;

    @Autowired
    private TokenService tokenService;

    /**
     * 获取当前用户菜单列表
     */
    @Log(module = "菜单管理", type = OperationType.QUERY, description = "获取用户菜单")
    @ApiOperation("获取用户菜单")
    @GetMapping("/getUserMenu")
    public AjaxResult getUserMenu() {
        try {
            LoginUser loginUser = tokenService.getLoginUser();
            if (loginUser == null) {
                return AjaxResult.error(HttpStatus.UNAUTHORIZED, "用户未登录或会话已过期");
            }

            if (loginUser.getUser() == null || loginUser.getUser().getId() == null) {
                return AjaxResult.error("用户信息不完整");
            }

            List<RouterVo> menuList = menuService.getUserMenuList(loginUser.getUser().getId());
            return AjaxResult.success(menuList);
        } catch (Exception e) {
            log.error("获取用户菜单失败", e);
            return AjaxResult.error("获取菜单失败：" + e.getMessage());
        }
    }
}
